package com.dnjn.happiness.biz.handler;

import com.dnjn.happiness.biz.domain.Communicate;
import com.dnjn.happiness.biz.domain.HistoryInterview;
import com.dnjn.happiness.biz.service.ICommunicateService;
import com.dnjn.happiness.biz.service.IHistoryInterviewService;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.emo.api.RemoteEmoUserService;
import com.dnjn.happiness.emo.api.model.EmoUserVO;
import com.dnjn.happiness.system.api.RemoteConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component("historyInterviewHandler")
public class HistoryInterviewHandler {


    @Autowired
    private RemoteEmoUserService emoUserService;

    @Autowired
    private RemoteConfigService configService;

    @Autowired
    private IHistoryInterviewService historyInterviewService;

    @Autowired
    private ICommunicateService communicateService;

    /**
     * 定时统计每天需沟通人员列表
     */
    public void countInterviewNumber(){
        int num = 0;
        R<String> resultNum = configService.getThresholdDateaApi(SecurityConstants.INNER);
        if (StringUtils.isNotNull(resultNum) && StringUtils.isNotNull(resultNum.getData())) {
            num = Integer.parseInt(resultNum.getData());
        }else {
            return;
        }
//        List<User> userList = emoUserService.lambdaQuery().eq(User::getDeleted, "0").list();
        List<EmoUserVO> userList = new ArrayList<>();
        R<List<EmoUserVO>> voResult = emoUserService.selectUserList(SecurityConstants.INNER);
        if(StringUtils.isNotNull(voResult) && StringUtils.isNotNull(voResult.getData())){
            userList = voResult.getData();
        }

        if (null == userList || userList.size() == 0){
            return;
        }
        for (EmoUserVO user : userList) {
//            SysUser sysUser = sysUserService.selectUserInfo(user.getUserId());
//            if (null == sysUser || null == user.getHappinessScore() || 1L == user.getUserId()){
//                continue;
//            }
            Integer happinessScore = user.getHappinessScore().intValue();
            //需要沟通
            if (happinessScore < num) {
                //先去查询昨天有没有该用户  没有新增
                LocalDate localDate = LocalDate.now();
                //
                HistoryInterview oldHistoryInterview = historyInterviewService.lambdaQuery().eq(HistoryInterview::getCurrDate, localDate.minusDays(1)).eq(HistoryInterview::getUserId, user.getUserId()).one();
                if (null == oldHistoryInterview){
                    HistoryInterview dto = new HistoryInterview()
                            .setCurrDate(localDate)
                            .setUserId(user.getUserId());
                    historyInterviewService.save(dto);
                }
                //同时落库沟通表,需沟通
                Long count = communicateService.lambdaQuery().eq(Communicate::getUserId, user.getUserId()).and((Wrappers) -> {
                    Wrappers.eq(Communicate::getCommunicateStatus, "0").or().eq(Communicate::getCommunicateStatus, "1");
                }).count();
                if (count < 1){
                    Communicate communicate = new Communicate();
                    communicate.setUserId(user.getUserId());
                    communicate.setCommunicateType("INTERVIEW");
                    communicate.setCommunicateStatus("0");
                    communicateService.saveCommunicate(communicate);
                }
            }

        }

    }
}
